package com.stox.client.ui.javafx.chartingy.persistence.chart.model;

import java.util.ArrayList;
import java.util.List;

import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;

import com.stox.client.api.core.Client;
import com.stox.shared.charting.model.ChartGroup;
import com.stox.shared.charting.model.SavedChart;

public class OChartGroup extends ChartGroup {
	
	public static final ListProperty<OChartGroup> ALL = new SimpleListProperty<OChartGroup>(FXCollections.observableArrayList());
	
	private static boolean loaded = false;
	public static synchronized void loadAll(){
		if(!loaded){
			loaded = true;
			List<ChartGroup> chartGroups = Client.CHARTING_CLIENT.findAllChartGroups();
			List<OChartGroup> oChartGroups = new ArrayList<OChartGroup>(chartGroups.size());
			for(ChartGroup chartGroup : chartGroups){
				OChartGroup oChartGroup = new OChartGroup();
				oChartGroup.setId(chartGroup.getId());
				oChartGroup.setName(chartGroup.getName());
				oChartGroup.setCharts(chartGroup.getCharts());
				oChartGroups.add(oChartGroup);
			}
			ALL.setAll(oChartGroups);
		}
	}
	
	private StringProperty nameProperty = new SimpleStringProperty();
	private ListProperty<SavedChart> chartsProperty = new SimpleListProperty<SavedChart>(FXCollections.observableArrayList());
	
	@Override
	public List<SavedChart> getCharts() {
		return chartsProperty.get();
	}
	
	@Override
	public void setCharts(List<SavedChart> charts) {
		chartsProperty.get().setAll(charts);
	}
	
	public ListProperty<SavedChart> chartsProperty(){
		return chartsProperty;
	}
	
	@Override
	public String getName() {
		return nameProperty.get();
	}
	
	@Override
	public void setName(String name) {
		nameProperty.set(name);
		super.setName(name);
	}
	
	public StringProperty nameProperty(){
		return nameProperty;
	}
}
